30 Lecture

CS201

Midterm & Final Term Short Notes

Reference data type

In C++, a reference is a data type that refers to another variable. It is used to create an alias or alternate name for an existing variable, allowing the original variable to be accessed or modified using a different name. Unlike pointers, refe


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a reference data type in C++? a) It is a variable that stores the memory address of another variable b) It is a variable that refers to another variable by name c) It is a variable that can be assigned a null value d) It is a variable that cannot be passed to a function

Answer: b

  1. What is the difference between a pointer and a reference in C++? a) Pointers can be reassigned to point to different variables, while references cannot. b) References can be null, while pointers cannot. c) Pointers are used to pass variables by reference, while references are used to pass variables by value. d) There is no difference between a pointer and a reference in C++.

Answer: a

  1. Can a reference be declared without being initialized? a) Yes, a reference can be declared without being initialized. b) No, a reference must be initialized when it is declared. c) It depends on the data type of the reference. d) It depends on the scope of the reference.

Answer: b

  1. What is the benefit of passing parameters by reference in a function? a) It saves memory by not creating a copy of the variable. b) It allows the function to modify the original variable. c) It makes the code more readable. d) It makes the code faster.

Answer: b

  1. What happens if a reference is assigned to a new variable? a) The original variable is deleted. b) The new variable becomes an alias for the original variable. c) A new copy of the original variable is created. d) The program crashes.

Answer: b

  1. Can a reference be used as a return type for a function? a) Yes, a reference can be used as a return type for a function. b) No, a reference cannot be used as a return type for a function. c) It depends on the data type of the reference. d) It depends on the scope of the reference.

Answer: a

  1. What is the syntax for declaring a reference variable in C++? a) int& x; b) int* x; c) int x&; d) int& x = y;

Answer: d

  1. Can a reference refer to a const variable? a) Yes, a reference can refer to a const variable. b) No, a reference cannot refer to a const variable. c) It depends on the data type of the reference. d) It depends on the scope of the reference.

Answer: a

  1. What is the difference between a const reference and a non-const reference? a) A const reference cannot be modified, while a non-const reference can. b) A non-const reference cannot be modified, while a const reference can. c) There is no difference between a const reference and a non-const reference. d) A const reference cannot refer to a non-const variable.

Answer: a

  1. Can a reference refer to a temporary object? a) Yes, a reference can refer to a temporary object. b) No, a reference cannot refer to a temporary object. c) It depends on the data type of the reference. d) It depends on the scope of the reference.

Answer: a



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a reference variable in C++? How is it different from a pointer? Answer: A reference variable in C++ is an alias to an already existing variable. It is declared using an ampersand (&) symbol. A reference variable is different from a pointer in that it cannot be null and cannot be reassigned to point to another object.

  2. Can a reference be returned from a function in C++? Answer: Yes, a reference can be returned from a function in C++. This can be useful in cases where we want to modify the value of an existing variable using a function.

  3. What is the purpose of using a reference as a function parameter in C++? Answer: Using a reference as a function parameter in C++ allows us to modify the value of the original variable that is being passed to the function, rather than just making a copy of the variable.

  4. How is a reference different from a constant reference in C++? Answer: A constant reference in C++ is a reference that cannot be modified. This means that any attempt to modify the value of the referenced variable will result in a compilation error.

  5. What is a reference variable in C++ used for? Answer: A reference variable in C++ is typically used to provide an alternative name for an existing variable. It can also be used to pass variables by reference to functions, which can be more efficient than passing by value.

  6. Can a reference be used to refer to a pointer variable in C++? Answer: Yes, a reference can be used to refer to a pointer variable in C++. This can be useful in cases where we want to modify the value of the pointer variable using a function.

  7. How is a reference variable initialized in C++? Answer: A reference variable in C++ must be initialized when it is declared. This initialization binds the reference to the variable being referenced.

  8. What is the difference between a reference and a copy in C++? Answer: A reference in C++ is an alias to an existing variable, while a copy is a separate instance of the variable. Modifying a reference modifies the original variable, while modifying a copy does not affect the original variable.

  9. Can a reference be used to refer to an object in C++? Answer: Yes, a reference can be used to refer to an object in C++. This can be useful in cases where we want to modify the value of an object using a function.

  10. What is the difference between a reference and a pointer in C++? Answer: A reference in C++ is an alias to an existing variable, while a pointer is a variable that stores the memory address of another variable. A reference cannot be null and cannot be reassigned to point to another object, while a pointer can be null and can be reassigned to point to another object.

Reference data type is a data type that stores the address of another variable. It is also known as a pointer variable. In C++, a reference is a type of data that refers to another object or variable. References are used to create aliases for existing variables. When a reference is created, it must be initialized with an existing object or variable. After initialization, any changes made to the original object will also be reflected in the reference variable. In C++, references are denoted using the ampersand (&) symbol. The primary benefit of references is that they allow for more efficient and readable code. Rather than passing large objects or structures by value, which requires a copy of the object to be created, references can be passed by reference, which allows the function to modify the original object directly. References can also be used as return types for functions, allowing a function to return multiple values. They can also be used to create const references, which are read-only references that prevent the original object from being modified. In addition to references, C++ also provides pointers as a way to work with memory addresses. However, pointers are more powerful and more error-prone than references, as they allow for direct manipulation of memory addresses. For this reason, it is generally recommended to use references when possible and pointers only when necessary. Overall, references are a powerful feature of C++ that allow for efficient and readable code, and should be used whenever possible to create aliases for existing variables and to pass variables by reference.